import java.util.Stack;

/**
 * @Author: Aerospace
 * @Date: 2021/5/18 14:27
 * @Description: Dream begins
 *
 * 给你一个字符串表达式 s ，请你实现一个基本计算器来计算并返回它的值。
 *
 * 整数除法仅保留整数部分。
 */
public class Calculate {
    public static void main(String[] args) {
        int result = Calculate("3/2");
        System.out.println(result);
    }
    public static int Calculate(String s) {
        char cursign = '+';
        Stack<Integer> numStack = new Stack<Integer>();
        int curnum = 0;
        int result = 0;
        for (int i = 0; i < s.length(); i++) {
            char cur = s.charAt(i);
            if (cur >= '0'){
                //记录当前数字。先减，防溢出
                curnum = curnum*10 - '0' + cur;
            }
            if((cur<'0' && cur!=' ')|| i == s.length()-1){
                switch (cursign){
                    case '+': numStack.push(curnum); break;
                    case '-': numStack.push(-curnum); break;
                    case '*': numStack.push(numStack.pop()*curnum); break;
                    case '/': numStack.push(numStack.pop()/curnum); break;
                }
                //记录当前符号
                cursign = cur;
                curnum = 0;
            }
        }
        while (!numStack.isEmpty()){
            result += numStack.pop();
        }
        return result;
    }
}
